/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.compomics.fastaconverter.gui;

import com.compomics.fastaconverter.DNAtoAAConverter;
import com.compomics.fastaconverter.triplet.TripletConverter;
import com.compomics.fastaconverter.triplet.TripletFactory;
import java.awt.Component;
import java.awt.Container;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JFrame;

/**
 *
 * @author Kenneth
 */
public class Preview extends javax.swing.JFrame {

    private static int treshold;
    private static boolean include;
    private BufferedWriter bw;
    private BufferedReader br;

    /**
     * Creates new form Preview
     */
    public Preview(int treshold, boolean include, MainWindow mainWindow) {
        this.setLocationRelativeTo(mainWindow);
        Preview.treshold = treshold;
        Preview.include = include;
        initComponents();
        inputExampleFasta.setTabSize(100);
        inputExampleFasta.setCaretPosition(0);
        inputExampleFasta.setColumns(100);
        inputExampleFasta.validate();
        outputExampleFasta.setTabSize(100);
        outputExampleFasta.setColumns(100);
        outputExampleFasta.validate();
        removeMinMaxClose(this.getParent());
        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        this.setVisible(true);
    }

    public void removeMinMaxClose(Component comp) {
        if (comp instanceof JButton) {
            String accName = ((JButton) comp).getAccessibleContext().getAccessibleName();
            System.out.println(accName);
            if (accName.equals("Maximize") || accName.equals("Iconify")
                    || accName.equals("Close")) {
                comp.getParent().remove(comp);
            }
        }
        if (comp instanceof Container) {
            Component[] comps = ((Container) comp).getComponents();
            for (int x = 0, y = comps.length; x < y; x++) {
                removeMinMaxClose(comps[x]);
            }
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollBar1 = new javax.swing.JScrollBar();
        jScrollPane1 = new javax.swing.JScrollPane();
        inputExampleFasta = new javax.swing.JTextArea();
        jScrollPane2 = new javax.swing.JScrollPane();
        outputExampleFasta = new javax.swing.JTextArea();
        launchPreview = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Preview Details of Settings");
        setMaximumSize(new java.awt.Dimension(598, 412));
        setMinimumSize(new java.awt.Dimension(598, 412));
        setResizable(false);

        inputExampleFasta.setColumns(200);
        inputExampleFasta.setLineWrap(true);
        inputExampleFasta.setRows(5);
        inputExampleFasta.setText(">Example_Sequence\nGAATTCCGGAAGCGAGCAAGAGATAAGTCCTGGCATCAGATACAGTTGGAGATAAGGACG\nGACGTGTGGCAGCTCCCGCAGAGGATTCACTGGAAGTGCATTACCTATCCCATGGGAGCC\nATGGAGTTCGTGGCGCTGGGGGGGCCGGATGCGGGCTCCCCCACTCCGTTCCCTGATGAA\nGCCGGAGCCTTCCTGGGGCTGGGGGGGGGCGAGAGGACGGAGGCGGGGGGGCTGCTGGCC\nTCCTACCCCCCCTCAGGCCGCGTGTCCCTGGTGCCGTGGGCAGACACGGGTACTTTGGGG\nACCCCCCAGTGGGTGCCGCCCGCCACCCAAATGGAGCCCCCCCACTACCTGGAGCTGCTG\nCAACCCCCCCGGGGCAGCCCCCCCCATCCCTCCTCCGGGCCCCTACTGCCACTCAGCAGC\nGGGCCCCCACCCTGCGAGGCCCGTGAGTGCGTCATGGCCAGGAAGAACTGCGGAGCGACG\nGCAACGCCGCTGTGGCGCCGGGACGGCACCGGGCATTACCTGTGCAACTGGGCCTCAGCC\nTGCGGGCTCTACCACCGCCTCAACGGCCAGAACCGCCCGCTCATCCGCCCCAAAAAGCGC\nCTGCTGGTGAGTAAGCGCGCAGGCACAGTGTGCAGCCACGAGCGTGAAAACTGCCAGACA\nTCCACCACCACTCTGTGGCGTCGCAGCCCCATGGGGGACCCCGTCTGCAACAACATTCAC\nGCCTGCGGCCTCTACTACAAACTGCACCAAGTGAACCGCCCCCTCACGATGCGCAAAGAC\nGGAATCCAAACCCGAAACCGCAAAGTTTCCTCCAAGGGTAAAAAGCGGCGCCCCCCGGGG\nGGGGGAAACCCCTCCGCCACCGCGGGAGGGGGCGCTCCTATGGGGGGAGGGGGGGACCCC\nTCTATGCCCCCCCCGCCGCCCCCCCCGGCCGCCGCCCCCCCTCAAAGCGACGCTCTGTAC\nGCTCTCGGCCCCGTGGTCCTTTCGGGCCATTTTCTGCCCTTTGGAAACTCCGGAGGGTTT\nTTTGGGGGGGGGGCGGGGGGTTACACGGCCCCCCCGGGGCTGAGCCCGCAGATTTAAATA\nATAACTCTGACGTGGGCAAGTGGGCCTTGCTGAGAAGACAGTGTAACATAATAATTTGCA\nCCTCGGCAATTGCAGAGGGTCGATCTCCACTTTGGACACAACAGGGCTACTCGGTAGGAC\nCAGATAAGCACTTTGCTCCCTGGACTGAAAAAGAAAGGATTTATCTGTTTGCTTCTTGCT\nGACAAATCCCTGTGAAAGGTAAAAGTCGGACACAGCAATCGATTATTTCTCGCCTGTGTG\nAAATTACTGTGAATATTGTAAATATATATATATATATATATATATCTGTATAGAACAGCC\nTCGGAGGCGGCATGGACCCAGCGTAGATCATGCTGGATTTGTACTGCCGGAATTC\n");
        jScrollPane1.setViewportView(inputExampleFasta);

        outputExampleFasta.setColumns(20);
        outputExampleFasta.setLineWrap(true);
        outputExampleFasta.setRows(5);
        jScrollPane2.setViewportView(outputExampleFasta);

        launchPreview.setText("Update View");
        launchPreview.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                launchPreviewActionPerformed(evt);
            }
        });

        jLabel1.setText("Input");

        jLabel2.setText("Output");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 578, Short.MAX_VALUE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(launchPreview))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2))
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 175, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(4, 4, 4)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 175, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(launchPreview)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void launchPreviewActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_launchPreviewActionPerformed
        try {
            //clear the outputfield
            outputExampleFasta.setText("");
            File tempInput = File.createTempFile("tempInput", ".tmp");
            File tempOutput = File.createTempFile("tempOutput", ".tmp");
            //make sure files are deleted
            tempInput.deleteOnExit();
            tempOutput.deleteOnExit();
            //write it
            bw = new BufferedWriter(new FileWriter(tempInput));
            bw.write(inputExampleFasta.getText());
            bw.close();
            System.out.println(Preview.include);
            System.out.println(treshold);
            String[] cmd = new String[]{"-input", tempInput.getAbsolutePath(), "-output", tempOutput.getAbsolutePath(), "-include_unlikely", "" + Preview.include, "-include_unlikely_treshold", "" + treshold};
            DNAtoAAConverter.main(cmd);
            br = new BufferedReader(new FileReader(tempOutput));
            String strLine;
            //Read File Line By Line
            while ((strLine = br.readLine()) != null) {
                // Print the content on the textfield
                outputExampleFasta.append(strLine + "\n");
            }
            System.out.println("No conversion");
        } catch (IOException ex) {
            Logger.getLogger(Preview.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                bw.close();
            } catch (IOException ex) {
                bw = null;
            }
            try {
                br.close();
            } catch (IOException ex) {
                br = null;
            }
        }
    }//GEN-LAST:event_launchPreviewActionPerformed
    /**
     * @param args the command line arguments
     */
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextArea inputExampleFasta;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollBar jScrollBar1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JButton launchPreview;
    private javax.swing.JTextArea outputExampleFasta;
    // End of variables declaration//GEN-END:variables

    public static void setIncludeShort(boolean enabled) {
        Preview.include = enabled;
    }

    public static void setMinLength(Integer integer) {
        Preview.treshold = integer;
    }
}
